package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.admin.dto.LiquorCollect;
import org.linlinjava.litemall.db.domain.ILiquormallLiquor;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.service.ILiquormallLiquorService;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminCollectService {

    @Autowired
    private LitemallCollectService collectService;

    @Autowired
    private ILiquormallLiquorService liquorService;

    private List<LiquorCollect> convert2LiquorCollect(List<LitemallCollect> litemallCollects) {
        return litemallCollects.stream().map(litemallCollect -> {
            LiquorCollect liquorCollect = new LiquorCollect();
            liquorCollect.setId(litemallCollect.getId());
            liquorCollect.setUserId(litemallCollect.getUserId());
            liquorCollect.setValueId(litemallCollect.getValueId());
            liquorCollect.setAddTime(litemallCollect.getAddTime());

            ILiquormallLiquor iLiquormallLiquor = liquorService.queryById(litemallCollect.getValueId());
            if (Objects.isNull(iLiquormallLiquor)) {
                liquorCollect.setTitle("");
            } else {
                liquorCollect.setTitle(iLiquormallLiquor.getTitle());
            }
            return liquorCollect;
        }).collect(Collectors.toList());
    }

    public List<LiquorCollect> queryList(String userId, String valueId, Integer page, Integer limit, String sort, String order) {
        List<LitemallCollect> collectList = collectService.querySelective(userId, valueId, page, limit, sort, order);
        return convert2LiquorCollect(collectList);
    }

}
